package com.sanery.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.sanery.dao.UserDao;

public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	// 注册-验证用户名是否被占用
	public Integer getCommendUserName(String username) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int c = 0;
		try {
			conn = this.getConn();
			String sql = "select count(1) from students where stu_loginname=? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				c = rs.getInt(1);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			this.closeAll(rs, pstmt, conn);
		}
		return c;
	}

	// 注册-学校补全
	@Override
	public List<String> getCommendSchool(String school) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> list = new ArrayList<String>();
		try {
			conn = this.getConn();
			String sql = "select school_Name from schools where school_Name like ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, school + "%");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			this.closeAll(rs, pstmt, conn);
		}
		return list;
	}

	@Override
	public List<String> getProvince() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> list = new ArrayList<String>();
		try {
			conn = this.getConn();
			String sql = "select city_name from citys where city_parentid=-1";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			this.closeAll(rs, pstmt, conn);
		}
		return list;
	}

	@Override
	public List<String> getCity(String province) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> list = new ArrayList<String>();
		try {
			conn = this.getConn();
			String sql = "select city_name from citys where city_parentid=(select city_id from citys where city_name=?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, province);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			this.closeAll(rs, pstmt, conn);
		}
		return list;
	}

}
